{extend name="common/index" /}

{block name="title"}<title>{$title}</title>{/block}

{block name="action"}
<script type="text/html" id="barDemo">
    {if (!empty($edit_url))}
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    {/if}
    {if (!empty($del_url))}
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    {/if}
</script>
{/block}

{block name="footer"}
<script type="text/javascript">

    //初始化参数
    var cols = init_cols({$cols|json_encode|raw});
    var list_url = '{$list_url}';
    var edit_url = '{$edit_url?:""}';
    var del_url = '{$del_url?:""}';
    var update_url = '{$update_url?:""}';

    //初始化表格字段
    function init_cols(cols){
        var arr = [];
        cols.forEach((val) => {
            if(val.type == 'button'){
                val.templet = function (d) {
                    var str = '';
                    d.LAY_COL.option.forEach((v) => {
                        if(v[0] == d[d.LAY_COL['field']]){
                            var color = 'layui-btn-primary';
                            if(v[2] == ''){
                                color = '';
                            }else if(v[2] != undefined){
                                color = 'layui-btn-'+v[2];
                            }
                            str = '<button type="button" class="layui-btn layui-btn-xs '+color+'">'+v[1]+'</button>';
                        }
                    })
                    return str;
                }
            }else if(val.type == 'img'){
                val.templet = function (d) {
                    return '<img style="margin:auto;position:absolute;left:0;right:0;" src="'+d[d.LAY_COL['field']]+'" class="view_cp" width="auto" height="100%">';
                }
            }
            arr.push(val);
        });
        return cols;
    }

    layui.config({
        base: '/vendor/layui-admin/layui/modules/js/' //静态资源所在路径
    });

    layui.define(['table','form','admin','laydate'], function(exports) {
        var $ = layui.$;
        var table = layui.table;
        var form = layui.form;
        var laydate = layui.laydate;
        var table_filter = {};

            table.render({
                elem: '#table'
                , url: list_url
                , cols: [cols]
                , page: true
                , limit: 20
                , height: 'full-180'
                ,toolbar: true //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
                , text: {
                    none: '无数据' //默认：无数据。
                }
                , even: true
            });

            $('.lay-event-sync').click(function () {
                $.ajax({
                    url: '/backmanage/config/sync'
                    ,type: 'POST'
                    ,success(res){
                        if (res.code == 0){
                            table.reload("table");
                            layer.msg('同步成功');
                        } else{
                            layer.msg('同步失败');
                        }
                    }
                });
            });

            //tool 事件监听
            table.on('tool(table)', function(obj){
                var data = obj.data;
                if(obj.event === 'del'){
                    layer.confirm('真的删除行么', function(index){
                        layer.close(index);
                        layer.prompt({title: '请输入管理员密码', formType: 1}, function(pass, index){
                            $.ajax({
                                url: '/backmanage/config/check_admin'
                                ,data: {"code":pass}
                                ,type: 'POST'
                                ,success(res){
                                    if (res.code){
                                        $.ajax({
                                            url: del_url+'?id='+data.id
                                            ,data: {"id":data.id,"status":0}
                                            ,type: 'DELETE'
                                            ,success(res){
                                                if (res.code == 0){
                                                    table.reload("table");
                                                    layer.msg('删除成功');
                                                } else{
                                                    layer.msg('删除失败');
                                                }
                                            }
                                        });
                                        layer.close(index);
                                    } else{
                                        layer.msg('密码错误');
                                    }
                                }
                            });
                            layer.close(index);
                        });
                    });
                } else if(obj.event === 'edit'){
                    layer.open({
                        type: 2,
                        shadeClose: true,
                        shade: false,
                        maxmin: true, //开启最大化最小化按钮
                        area: ["100%", "100%"],
                        content: edit_url+'?id='+data.id,
                        end: function () {
                            //更新表格
                            $("#layui-icon-refresh").click();
                        }
                    });
                }
            });
            //筛选框监听
            form.on('submit(search)', function(data){
                table_filter = data.field;
                partition_id=data.field.partition_id
                reload();
                return false;
            });
            //重载 刷新
            $('#layui-icon-refresh').click(function () {
                reload();
            });
            //重载 刷新
            function reload(){
                table.reload('table',{
                    where: {
                        filter:table_filter
                    }
                });
            }
            exports('level', {});

            $(document).on('click','.view_cp',function(){
                layer.open({
                    type: 1,
                    title: false,
                    closeBtn: 0,
                    shadeClose: true,
                    area: 'auto',
                    content: '<img style="height:500px;" src="'+$(this).attr('src')+'">'
                });
            });
            //日期时间范围
            laydate.render({
                elem: '#time_start'
                ,type: 'datetime'
            });
            laydate.render({
                elem: '#time_end'
                ,type: 'datetime'
            });

        });

</script>
{/block}